<?php
class Admin_Install extends Admin_Controller{
	private $_path;
	function __construct(){
		parent::__construct();
		if($this->current_user->group_id != 1){
			$this->check_access();
		}
		$this->load->model('module_m');
	}
	public function index(){
		$this->action = 'new|'.site_url($this->uri->uri_string()).'/create|'.lang('global:addnew');
		
		$param['not_system'] = array(3);
		$model = $this->module_m->get_all($param);
		$this->template
			->set('model',$model)
			->set('action',$this->action)
			->build($this->controller.'/'.$this->method);
	}	
	
	public function status($module,$value){
		$data = array('status'=>($value+1)%2);
		$this->module_m->status($module,$data);
	}
	
	public function create($name = NULL){
		$model = $this->input->post();
		$this->form_validation->set_rules('module', 'lang:install:module', 'required');		
		$this->form_validation->set_rules('name', 'lang:install:name', 'required|max_length[20]|alpha_dash|is_unique[modules.id]');		
		
		
		$type =  substr($this->input->post('btnAction'), 0);
		
		if ( type_post($type) ) {
			if ($this->form_validation->run() == TRUE)
			{
				$skip = array('btnAction');
				foreach($skip as $obj){
					if(array_key_exists($obj,$model))
						unset($model[$obj]); 
				}
			$name = strtolower($this->input->post('name'));
			$module = $this->input->post('module');
			$this->load->file("sql/$module.php");
			$sql = query($name);
			
			foreach($sql as $line){
				$this->db->query($line);
			}
			$insert = insert($name);
			$this->db->query($insert);
			$insert = insert_data_module($name);
			$this->db->query($insert);
			redirect('admin/install');
			}
		}
		$this->action = 'saveclose|javascript:void(0)|'.lang('global:save_close');
		$modules =  array("news"=>"news");
		$this->template
			->set('modules',$modules)
			->set('model',$model)
			->set('action',$this->action)
			->append_css('table.css')
			->build($this->controller.'/form');
	}
}